<?php

namespace App\Services;

use Illuminate\Support\Facades\Storage;
use Image;
//use Str;

class RefereeService
{
    protected $allowed_ext = ["png", "jpg", "gif", 'jpeg'];

    protected $folder_name, $upload_path, $filename;

    public function setValue($folder, $file_prefix)
    {
        // 文件夹切割能让查找效率更高。
        $this->folder_name = "uploads/images/$folder/";
        // 拼接文件名，加前缀是为了增加辨析度，前缀可以是相关数据模型的 ID
        $this->filename = $file_prefix . '_poster';
    }

    public function getRecommendPoster($user_id, $vehicle_series_id, $qrcode, $poster, $text)
    {
        $this->setValue('referee/poster', $user_id . '_' . $vehicle_series_id);
        $img = Image::make($poster);
        $qr = Image::make($qrcode)->resize(200, 200);
        $img->insert($qr, 'bottom-center', 0, 80);
        $text = '推荐官：' . $text;
        $img->text($text, 350, 990, function ($font) {
            $font->file(resource_path('fonts/msyh.ttf'));
            $font->size(24);
            $font->color('#000000');
            $font->align('center');
            $font->valign('top');
        });
        $path = $this->folder_name . $this->filename . '.jpg';
        Storage::put($path, $img->stream('jpg', 60));
        $url = Storage::url($path);
        return $url;
    }
}
